import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  getToken
} from '@/api/cookie'
import About from '../views/About.vue'

import Layout from '../layout';
import store from '../store'
import login from '@/views/login'
import Store from '../store'
import register from '../views/register';
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
Vue.use(VueRouter)

const routes = [{
  path: "/login",
  name: 'login',
  component: login,
  hidden: true
},
{
  path: '/',
  redirect: {
    name: 'login'
  },
  hidden: true
},
{
  path: '/forget',
  name: 'forget',
  component: () => import('@/views/findPass.vue'),
  hidden: true
},
{
  path: '/',
  title: '仪表盘',
  component: Layout,
  redirect: '/home',
  children: [{
    path: '/home',
    component: () => import('@/views/Dashboard.vue'),
    name: 'home',
    meta: {
      title: '仪表盘',
      icon: "el-icon-document"
    }
  }]
},
{
  path: '/msbd',
  component: Layout,
  // redirect: '/home',
  name: 'msbd',
  title: '面试宝典',
  icon: "el-icon-document",
  children: [{
    path: '/list',
    component: () => import('@/views/msbd'),
    name: 'msbdList',
    meta: {
      title: '列表管理',
      icon: "el-icon-document"
    }
  },
  {
    path: '/classify',
    component: () => import('@/views/classify'),
    name: 'classify',
    meta: {
      title: '类别管理',
      icon: "el-icon-document"
    }
  }
  ]
},
{
  path: '/user',
  component: Layout,
  // redirect: '/home',
  name: 'msbd',
  title: '用户管理',
  icon: "el-icon-document",
  children: [{
    path: '/unusers',
    component: () => import('@/views/user/unusers'),
    name: 'unusers',
    meta: {
      title: '待审核用户',
      icon: "el-icon-document"
    }
  },
  {
    path: '/formal',
    component: () => import('@/views/user/formal'),
    name: 'formal',
    meta: {
      title: '正式用户',
      icon: "el-icon-document"
    }
  }
  ]
},
{
  path: '/permissions',
  title: '权限管理',
  component: Layout,
  redirect: '/permissions',
  children: [{
    path: '/permissions',
    component: () => import('@/views/permissions'),
    name: 'permissions',
    meta: {
      title: '权限管理',
      icon: "el-icon-document"
    }
  }]
},
{
  path: '/test',
  title: '测试',
  component: Layout,
  redirect: '/test',
  children: [{
    path: '/test',
    component: () => import('@/views/test'),
    name: 'test',
    meta: {
      title: '测试',
      icon: "el-icon-document"
    }
  }]
},
{
  path: '/register',
  name: 'register',
  component: register,
  hidden: true
},

{
  path: '/about',
  name: 'About',
  component: About,
  hidden: true
},
{
  path: '/profile',
  component: Layout,
  hidden: true,
  children: [{
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/profile')
  }]

}
]

const router = new VueRouter({
  routes
})
/* 配置进度条 */
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })
router.beforeEach(async (to, from, next) => {
  NProgress.start()	//显示/开启
  await store.dispatch('login/getrouter', router.options.routes);
  await store.dispatch('Logins/getitems', 'admin')
  await store.dispatch('login/setroute', store.state.Logins.items);
  const tok = ['/login', '/register']
  if (tok.includes(to.path)) {
    next()
  } else {
    if (getToken()) {
      try {
        await Store.dispatch('Logins/setinfo', { token: getToken() })
        await store.dispatch('Logins/getitems', store.state.Logins.roles)
        await store.dispatch('user/setroles', { menu: store.state.login.routers, items: store.state.Logins.items })
        next()
      } catch (error) { }
    } else {
      next('/login')
    }
    next();
  }
  next()
})
router.afterEach(() => {
  NProgress.done()	//消失/关闭
})

export default router